package com.stx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stx.mapper.RecordMapper;
import com.stx.pojo.Record;
import com.stx.service.IRecordService;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {
    @Override
    public BigDecimal findReset(Record record) {
        LambdaQueryWrapper<Record> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Record::getRuaNumber,record.getRuaNumber());
        wrapper.eq(Record::getPlaId,record.getPlaId());
        List<Record> records = baseMapper.selectList(wrapper);
        System.out.println(records);
        // 创建一个ArrayList来临时存储分数，方便后续操作
        List<Double> scoresList = new ArrayList<>();
        for (Record r : records) {
            scoresList.add(r.getRecScore());
        }
        System.out.println(scoresList);
        // 计算分数总和
        double sum = 0;
        for (Double score : scoresList) {
            sum += score;
        }
        System.out.println("avg"+sum);
        // 计算平均分，防止除数为0的情况，若没有获取到任何分数，返回0.0
//        double averageScore = scoresList.size() > 0? sum / scoresList.size() : 0.0;
        BigDecimal averageScore = new BigDecimal(scoresList.size() > 0? sum / scoresList.size() : 0.0).setScale(2, BigDecimal.ROUND_HALF_UP);
        return averageScore;
    }
}
